package controllers;

import java.util.ArrayList;
import java.util.List;

import models.GuidanceQuestion;
import models.GuidanceReply;
import models.Question;
import models.RegisteredUser;
import play.data.validation.Required;
import play.mvc.Controller;

public class Guidance extends Controller {

	/**
	 * This method takes the input questionContent and title and saves it in the
	 * database after passing the parameters questionContent and title to the
	 * constructor in the Models
	 * 
	 * @param title
	 * @param questionContent
	 *            ,
	 * @author Shayma
	 */

	public static void addQuestion(
			@Required(message = "Please Enter Your tittle") String title,
			@Required(message = "Please Enter Your question") String questionContent) {

		long userId = Long.parseLong(session.get("RegisteredUserId"));

		List<GuidanceQuestion> questions = GuidanceQuestion.findAll();
		GuidanceQuestion q = null;

		for (int i = 0; i < questions.size(); i++) {
			if (questions.get(i).getQuestionContent().equals(questionContent)) {
				q = questions.get(i);
			}
		}
		if (questionContent.equals("") && title.equals("")) {
			questionNothing();
		} else {
			if (validation.hasErrors()) {
				params.flash();
				validation.keep();
				questionNothing();
			} else {

				if (q == null) {
					models.GuidanceQuestion ques = new models.GuidanceQuestion(
							userId, title, questionContent).save();

					questionsPage(); // @author Mahmoud - calling
										// questionsPage after
										// saving question in
										// the database to view the page
										// with the new
										// question

				} else

					youEnteredDuplicate();

			}
		}

	}

	/**
	 * Method to view youEnteredDuplicate.html which tell the user that his/her
	 * entered a duplicate question
	 * 
	 * @author shayma hesham
	 */

	public static void youEnteredDuplicate() {
		render();
	}

	/**
	 * Method to view questionNothing.html which tell the user that his/her
	 * entered nothing in the question or in the tittle
	 * 
	 * @author shayma hesham
	 */

	public static void questionNothing() {
		render();
	}

	/**
	 * This method saves the reply of of the question in the database after
	 * passing the parameters reply content to the constructor in the database
	 * 
	 * @param questionId
	 * @param replyContent
	 * @author Shayma
	 **/

	public static void addReplyToQuestion(long questionId,
			@Required(message = "Please Enter Your reply") String replyContent) {

		long userId = Long.parseLong(session.get("RegisteredUserId"));
		List<GuidanceReply> replies = GuidanceReply.findAll();
		GuidanceReply r = null;

		for (int i = 0; i < replies.size(); i++) {
			if (replies.get(i).getReplyContent().equals(replyContent)) {
				r = replies.get(i);
			}
		}

		if (replyContent.equals("")) {
			enteredNthing();
		} else {

			if (validation.hasErrors()) {
				params.flash();
				validation.keep();
				enteredNthing();
			} else {

				if (r == null) {

					models.GuidanceReply rep = new models.GuidanceReply(userId,
							questionId, replyContent).save();

					models.GuidanceQuestion question = models.GuidanceQuestion
							.findById(questionId);
					question.hasReply = true;// @author Mahmoud - if reply is
												// entered: hasReply is set to
												// true
												// to set question
												// color to green in
												// questionsPage.html else stays
												// red
					question.save();

					repliesPage(questionId);

				} else
					replyDuplicate();

			}
		}
	}

	/**
	 * Method to view replyDuplicate.html which tell the user that his/her
	 * entered a duplicate reply
	 * 
	 * @author shayma hesham
	 */

	public static void replyDuplicate() {
		render();
	}

	/**
	 * Method to view enteredNthing.html which tell the user that his/her
	 * entered nothing in the reply
	 * 
	 * @author shayma hesham
	 */

	public static void enteredNthing() {
		render();

	}

	/**
	 * This method will get all questions made by all user when the "Ask for
	 * Guidance button is pressed which is enabled for all users
	 * 
	 * @author Mahmoud
	 */
	public static void questionsPage() {

		List<GuidanceQuestion> questions = GuidanceQuestion.findAll();
		if (questions.size() > 0)
			render(questions);
		else
			render();

	}

	/**
	 * this method will get all the replies made on a certain question all users
	 * on the web-site can view any question with it's replies
	 * 
	 * @param questionId
	 * @author Ghada Kades
	 */

	public static void repliesPage(long questionId) {

		List<GuidanceReply> replies = GuidanceReply.find("questionId=? ",
				questionId).fetch();
		GuidanceQuestion question = GuidanceQuestion.findById(questionId);
		RegisteredUser asker = RegisteredUser.findById(question.userId);
		List<RegisteredUser> repliers = new ArrayList<RegisteredUser>();
		for (int i = 0; i < replies.size(); i++) {

			RegisteredUser replier = RegisteredUser
					.findById(replies.get(i).userId);
			repliers.add(i, replier);
		}
		if (replies.size() > 0)
			render(asker, question, repliers, replies);
		else
			render(asker, question);

	}

}
